package ch02sl.sz.sz3;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.net.MalformedURLException;
import java.util.Iterator;

public class XMLParseDel {

    public  void showPhoneInfo() {
        Document doc = null;
        try {
            SAXReader saxReader = new SAXReader();
            doc =  saxReader.read(new File("src/test/java/ch02sl/sz/PhoneInfo.xml"));
        }
         catch (DocumentException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        Element root =  doc.getRootElement();
        // 删除Brand节点中品牌名为"SAMSUNG"的节点
        for (Iterator<Element> it = root.elementIterator("Brand"); it.hasNext(); ) {
            Element brand = it.next();
            if ("三星".equals(brand.attributeValue("name"))) {
                root.remove(brand);
            }
        }
        for (Iterator iterator = root.elementIterator(); iterator.hasNext(); ) {
           Element brandEle = (Element) iterator.next();
            System.out.println("品牌："+brandEle.attributeValue("name"));
            for (Iterator itType = brandEle.elementIterator();itType.hasNext();){
                Element typeEle = (Element) itType.next();
                System.out.println("\t型号："+typeEle.attributeValue("name"));
            }
        }
    }
    public static void main(String[] args) {
        XMLParseDel parse = new XMLParseDel();
        parse.showPhoneInfo();
    }
}
